using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using System.Collections.Generic;
namespace IGlobalist.Cms.Admin.Control
{
    public partial class UserList : AdminBaseUserControl
    {
        public event EventHandler EditUser;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void DoBindUser(object sender, EventArgs e)
        {
            ClassList ucClassList = (ClassList)sender;
            long classID = 0;
            classID = Convert.ToInt64(ucClassList.SelectedValue);
            ///Tuong changed
            if (classID == 0) classID = (long)Session[SessionVariable.Clazz.CurrentID];
            BindUser(classID);
        }

        public void BindUser(long classID)
        {            
            ICmsClass clazz = ClassController.GetClass(classID);
            Session[SessionVariable.Clazz.CurrentID] = classID;
            lblStatus.Text = String.Format("List User of Class {0}", clazz.Name);
            gvObject.DataSource = UserController.GetUserList(classID);
            gvObject.DataBind();
            ViewState["UserID"] = null;
        }

        public void DoDelete(object sender, EventArgs e)
        {
            if (ViewState["UserID"] != null) UserController.DeleteUser(Convert.ToInt64(ViewState["UserID"]));
            BindUser(Convert.ToInt64(ViewState["ClassID"]));
        }

        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;

        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
            BindUser(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));

        }
        public string SelectedID
        {
            get { return Convert.ToString(ViewState["UserID"]); }
            set { ViewState["UserID"] = value; }
        }

        protected void gvObject_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            long ID = Convert.ToInt64(e.CommandArgument);
            if (e.CommandName == "editUser")
            {
                ViewState["UserID"] = ID;
                if (EditUser != null)
                {
                    EditUser(this, new EventArgs());
                }
            }

            if (e.CommandName == "delUser")
            {
                UserController.DeleteUser(ID);
                BindUser(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
            }
        }

       
    }
}